//
//  CZPhotoSelectorCell.swift
//  broser
//
//  Created by 李伟鹏 on 16/5/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

protocol CZPhotoSelectorCellDelegate: NSObjectProtocol {
    func photoSelectorCellDidClickAddButton(cell: CZPhotoSelectorCell)
    
    func photoSelectroCellDidClickDeleteButton(cell: CZPhotoSelectorCell)
}

class CZPhotoSelectorCell: UICollectionViewCell{
    
    weak var delegate: CZPhotoSelectorCellDelegate?
    
    var image: UIImage? {
        didSet {
            // 将图片设置给加号按钮
            addButton.setImage(image, forState: UIControlState.Normal)
            deleteButton.hidden = false 
        }
    }
    
    func setupAddButton(){
        addButton.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        deleteButton.hidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepreUI()
    }
    
    private func prepreUI() {
        // 添加控件
        contentView.addSubview(addButton)
        contentView.addSubview(deleteButton)
        
        // 添加约束
        addButton.translatesAutoresizingMaskIntoConstraints = false
        deleteButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 加号按钮
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[ab]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["ab" : addButton]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[ab]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["ab" : addButton]))
        
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[db]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["db" : deleteButton]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[db]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["db" : deleteButton]))
    }
    
    @objc private func didClickAddButton() {
        delegate?.photoSelectorCellDidClickAddButton(self)
    }
    
    @objc private func didClickDeleteButton() {
        delegate?.photoSelectroCellDidClickDeleteButton(self)
    }
    
    // MARK: - 懒加载
    /// 加号
    private lazy var addButton: UIButton = {
        let button = UIButton()
        
        // 设置图片
        button.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
         button.addTarget(self, action: "didClickAddButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 删除
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
         button.addTarget(self, action: "didClickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        
        return button
    }()
}
